package com.bwie.product.task;

import com.bwie.common.doamin.vo.ProductVO;
import com.bwie.product.es.ProductES;
import com.bwie.product.esMapper.ProductEsMapper;
import com.bwie.product.mapper.ProductMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 产品同步ES
 *
 * @author <a href="mailto:2943147140@qq.com">刘洋朔</a>
 * @version 1.0
 * @description: 产品同步ES
 * @date 2025/9/9 14:30
 */
@Component("ProductTaskES")
public class ProductTaskES {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductEsMapper productEsMapper;

    //使用定时器每隔30S同步一次商品信息（3分）
    @Scheduled(cron = "0/30 * * * * *")
    public void toProductEs(){
        //获取所有商品信息
        List<ProductVO> productVOList = productMapper.selAllProductList();
        //进行数据同步
        List<ProductES> productESList = productVOList.stream().map(productVO -> {
            ProductES productES = new ProductES();
            BeanUtils.copyProperties(productVO, productES);
            productES.setId(productVO.getId() + "");
            return productES;
        }).collect(Collectors.toList());
        //进行添加
        productEsMapper.insertBatch(productESList);
    }
}
